package com.example.firstapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.Editable;
import android.text.InputType;
import android.view.View;

import com.example.firstapp.adapter.MoveAndSwipedAdapter;
import com.example.firstapp.manager.IItemHelper;
import com.example.firstapp.manager.TouchCallBack;

import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    AppCompatEditText custom_kb_remark;

    AppCompatEditText custom_kb_calculate_money;

    AppCompatButton custom_kb_complete, custom_kb_point, custom_kb_delete, custom_kb_zero, custom_kb_lose,
            custom_kb_three, custom_kb_two, custom_kb_one, custom_kb_four, custom_kb_five, custom_kb_six,
            custom_kb_add, custom_kb_date, custom_kb_nine, custom_kb_eight, custom_kb_seven;

    private BigDecimal result;

    QMUITopBarLayout topbar_title;

    RecyclerView recycle;

    MoveAndSwipedAdapter moveAndSwipedAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        QMUIStatusBarHelper.translucent(this);
        initViews();
    }

    private void initViews() {
        custom_kb_remark = findViewById(R.id.custom_kb_remark);
        custom_kb_calculate_money = findViewById(R.id.custom_kb_calculate_money);
        custom_kb_complete = findViewById(R.id.custom_kb_complete);
        custom_kb_point = findViewById(R.id.custom_kb_point);
        custom_kb_delete = findViewById(R.id.custom_kb_delete);
        custom_kb_zero = findViewById(R.id.custom_kb_zero);
        custom_kb_lose = findViewById(R.id.custom_kb_lose);
        custom_kb_three = findViewById(R.id.custom_kb_three);
        custom_kb_two = findViewById(R.id.custom_kb_two);
        custom_kb_one = findViewById(R.id.custom_kb_one);
        custom_kb_four = findViewById(R.id.custom_kb_four);
        custom_kb_five = findViewById(R.id.custom_kb_five);
        custom_kb_six = findViewById(R.id.custom_kb_six);
        custom_kb_add = findViewById(R.id.custom_kb_add);
        custom_kb_date = findViewById(R.id.custom_kb_date);
        custom_kb_nine = findViewById(R.id.custom_kb_nine);
        custom_kb_eight = findViewById(R.id.custom_kb_eight);
        custom_kb_seven = findViewById(R.id.custom_kb_seven);
        topbar_title = findViewById(R.id.topbar_title);
        recycle = findViewById(R.id.recycle);

        initAttr();
    }

    private void initAttr() {
        custom_kb_calculate_money.setInputType(InputType.TYPE_NULL);
        custom_kb_complete.setOnClickListener(this);
        custom_kb_point.setOnClickListener(this);
        custom_kb_delete.setOnClickListener(this);
        custom_kb_zero.setOnClickListener(this);
        custom_kb_lose.setOnClickListener(this);
        custom_kb_three.setOnClickListener(this);
        custom_kb_two.setOnClickListener(this);
        custom_kb_one.setOnClickListener(this);
        custom_kb_four.setOnClickListener(this);
        custom_kb_five.setOnClickListener(this);
        custom_kb_six.setOnClickListener(this);
        custom_kb_add.setOnClickListener(this);
        custom_kb_date.setOnClickListener(this);
        custom_kb_nine.setOnClickListener(this);
        custom_kb_eight.setOnClickListener(this);
        custom_kb_seven.setOnClickListener(this);


        recycle.setLayoutManager(new GridLayoutManager(this,3));
        moveAndSwipedAdapter = new MoveAndSwipedAdapter(this);
        moveAndSwipedAdapter.addData(getDatas());
        recycle.setAdapter(moveAndSwipedAdapter);

        ItemTouchHelper touchHelper = new ItemTouchHelper(new TouchCallBack(moveAndSwipedAdapter));
        touchHelper.attachToRecyclerView(recycle);


        topbar_title.setTitle("主界面");
        topbar_title.setSubTitle("我是副标题");
    }

    @Override
    public void onClick(View v) {
        String number = "";
        switch (v.getId()) {
            case R.id.custom_kb_one:
                number = "1";
                custom_kb_calculate_money.getText().append(isLegal(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_two:
                number = "2";
                custom_kb_calculate_money.getText().append(isLegal(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_three:
                number = "3";
                custom_kb_calculate_money.getText().append(isLegal(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_four:
                number = "4";
                custom_kb_calculate_money.getText().append(isLegal(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_five:
                number = "5";
                custom_kb_calculate_money.getText().append(isLegal(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_six:
                number = "6";
                custom_kb_calculate_money.getText().append(isLegal(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_seven:
                number = "7";
                custom_kb_calculate_money.getText().append(isLegal(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_eight:
                number = "8";
                custom_kb_calculate_money.getText().append(isLegal(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_nine:
                number = "9";
                custom_kb_calculate_money.getText().append(isLegal(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_zero:
                number = "0";
                custom_kb_calculate_money.getText().append(isLegal(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_add:
                number = "+";
                custom_kb_calculate_money.append(clickAddOrLose(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_lose:
                number = "-";
                custom_kb_calculate_money.append(clickAddOrLose(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_point:
                number = ".";
                custom_kb_calculate_money.append(clickPoint(custom_kb_calculate_money.getText().toString().trim(), number));
                break;
            case R.id.custom_kb_delete:
                number = "";
                Editable editable = custom_kb_calculate_money.getText();
                if (editable.length() > 0) {
                    editable.delete(editable.length() - 1, editable.length());
                }
                custom_kb_calculate_money.setText(editable);
                break;
            case R.id.custom_kb_complete:
                custom_kb_calculate_money.setText(calculate(custom_kb_calculate_money.getText().toString().trim()));
                break;
        }
    }


    private List<String> getDatas() {
        List<String> datas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            datas.add("" + i);
        }
        return datas;
    }


    /**
     * 点击完成，相等于等号
     *
     * @param number：要计算的字符串
     * @return
     */
    private String calculate(String number) {
        String[] numbers;
        if (number.length() > 0) {
            if (number.charAt(0) == '-') {
                //第一个数字为负数
                String allNum = number.substring(1, number.length());
                result = addOrLose(allNum, "-");
            } else {
                result = addOrLose(number, "+");
            }

            return result.toPlainString();//将科学计数法转换为普通计数
        }
        return "";
    }

    /**
     * 加减法
     *
     * @param number：要计算的数字
     * @param operator：正数还是负数:"+"表示正数，"-"表示负数
     * @return
     */
    private BigDecimal addOrLose(String number, String operator) {
        String[] numbers;
        if (number.contains("-")) {
            numbers = number.split("-");
            if (numbers.length == 2) {
                BigDecimal decimal1 = new BigDecimal(operator + numbers[0]);
                BigDecimal decimal2 = new BigDecimal(numbers[1]);
                result = decimal1.subtract(decimal2).stripTrailingZeros();
            } else {
                BigDecimal decimal1 = new BigDecimal(operator + numbers[0]);
                BigDecimal decimal2 = new BigDecimal(0.00);
                result = decimal1.subtract(decimal2).stripTrailingZeros();//相减操作，末尾小数去掉0
            }
        }

        if (number.contains("+")) {
            numbers = number.split("\\+");
            if (numbers.length == 2) {
                BigDecimal decimal1 = new BigDecimal(numbers[0]);
                BigDecimal decimal2 = new BigDecimal(numbers[1]);
                result = decimal1.add(decimal2).stripTrailingZeros();
            } else {
                BigDecimal decimal1 = new BigDecimal(numbers[0]);
                BigDecimal decimal2 = new BigDecimal(0.00);
                result = decimal1.add(decimal2).stripTrailingZeros();//相加操作，末尾小数去掉0
            }
        }
        return result;
    }

    /**
     * 点击"."符号做的操作
     *
     * @param allNumber:输入框内的全部字符
     * @param clickNumber：.
     * @return
     */
    private String clickPoint(String allNumber, String clickNumber) {
        if (allNumber.length() == 0) {
            return "0" + clickNumber;
        } else {
            if (allNumber.charAt(0) == '-') {
                //第一个数字为负数
                String allNum = allNumber.substring(1, allNumber.length());
                return clickPointVerify(allNum, clickNumber);
            } else {
                return clickPointVerify(allNumber, clickNumber);
            }
        }
    }

    /**
     * 点击"."符号做的校验
     *
     * @param allNumber:输入框内的全部字符
     * @param clickNumber：.
     * @return
     */
    private String clickPointVerify(String allNumber, String clickNumber) {
        if (allNumber.contains("-")) {
            String[] nums = allNumber.split("-");
            if (nums.length == 2) {
                String result = "";
                for (String num : nums) {
                    if (num.contains(".")) {
                        result = "";
                    } else {
                        result = clickNumber;
                    }
                }
                return result;
            } else {
                if (nums[0].contains(".")) {
                    return "";
                } else {
                    return clickNumber;
                }
            }
        } else if (allNumber.contains("+")) {
            String[] nums = allNumber.split("\\+");
            if (nums.length == 2) {
                String result = "";
                for (String num : nums) {
                    if (num.contains(".")) {
                        result = "";
                    } else {
                        result = clickNumber;
                    }
                }
                return result;
            } else {
                if (nums[0].contains(".")) {
                    return "";
                } else {
                    return clickNumber;
                }
            }

        } else if (allNumber.contains(".")) {
            return "";
        }

        return clickNumber;

    }

    /**
     * 点击"+" | "-"符号做的操作
     *
     * @param allNumber:输入框内的全部字符
     * @param clickNumber：点击的按键
     * @return
     */
    private String clickAddOrLose(String allNumber, String clickNumber) {
        if (allNumber.length() == 0) {
            return "0" + clickNumber;
        } else {
            char[] nums = allNumber.toCharArray();

            if (nums[0] == '-') {
                //说明第一个要计算的数字为负数
                for (int i = 1; i < nums.length; i++) {
                    if (nums[i] == '-' || nums[i] == '+') {
                        //除了负数还包含一个运算符
                        return "";
                    }
                }
                return clickNumber;
            } else {
                //第一个计算的不是负数
                if (allNumber.contains("-") || allNumber.contains("+")) {
                    return "";
                } else {
                    return clickNumber;
                }
            }

        }
    }

    /**
     * 校验数字合法性
     *
     * @param allNumber：输入框内的全部字符
     * @param clickNumber：点击的按键
     * @return
     */
    private String isLegal(String allNumber, String clickNumber) {
        String legalNum = "";
        if (allNumber.length() == 0) {//如果当前输入框没有字符,直接返回该数字
            return clickNumber;
        } else {
            if (allNumber.charAt(0) == '-') { //如果是负数
                String allNum = allNumber.substring(1, allNumber.length());//截取负数运算符之后的数字
                legalNum = getLegalNum(allNum, clickNumber);
            } else {
                legalNum = getLegalNum(allNumber, clickNumber);
            }
        }
        return legalNum;
    }

    /**
     * 获取校验后的合法数字
     *
     * @param allNumber：输入框中的数字
     * @param clickNumber：点击的数字
     * @return
     */
    private String getLegalNum(String allNumber, String clickNumber) {
        String[] numbers;
        String legalNum = "";
        if (allNumber.contains("-")) { //如果当前输入框包含运算符
            numbers = allNumber.split("-");//分割
            if (numbers.length == 2) { //遍历校验每个数字的合法性
                for (String num : numbers) {
                    legalNum = numVerify(num, clickNumber);
                }
            } else {
                legalNum = clickNumber;
            }

        } else if (allNumber.contains("+")) {
            numbers = allNumber.split("\\+");
            if (numbers.length == 2) {
                for (String num : numbers) {
                    legalNum = numVerify(num, clickNumber);
                }
            } else {
                legalNum = clickNumber;
            }

        } else {
            legalNum = numVerify(allNumber, clickNumber);
        }
        return legalNum;
    }

    /**
     * 数字合法性校验
     *
     * @param allNumber
     * @param clickNumber
     * @return
     */
    private String numVerify(String allNumber, String clickNumber) {
        String resultNum = "";
        if (allNumber.contains(".")) {
            String[] nums = allNumber.split("\\.");
            if (nums.length == 2) {
                if (nums[1].length() == 2) { //小数位最多俩位
                    resultNum = "";
                } else {
                    resultNum = clickNumber;
                }

//                if (nums[0].length() == 9) { //整数位最多9位
//                    resultNum = "";
//                    if (nums[1].length() == 2) { //小数位最多俩位
//                        resultNum = "";
//                    } else {
//                        resultNum = clickNumber;
//                    }
//                } else {
//
//                }
            } else {

                resultNum = clickNumber;
//                if (nums[0].length() == 9) {
//                    resultNum = "";
//                } else {
//                    resultNum = clickNumber;
//                }
            }
        } else {
            if (allNumber.length() == 9) {//整数位最多9位
                resultNum = "";
            } else {
                resultNum = clickNumber;
            }

        }
        return resultNum;
    }
}
